//
//  FFTransferCell.m
//  LiMaoTransfer
//
//  Created by tt on 2020/9/18.
//

#import "FFTransferCell.h"
#import "FFTransferContent.h"
#import "FFTransferModule.h"
#import "FFTransferDetailVC.h"
@interface FFTransferCell ()

@property(nonatomic,strong) UIView *splitLineView; // 分割线

@property(nonatomic,strong) UIImageView *iconImgView; // icon

@property(nonatomic,strong) UILabel *amountLbl; // 转账金额

@property(nonatomic,strong) UILabel *remarkLbl; // 转账备注

@property(nonatomic,strong) UILabel *copyrightLbl; // 版权

@end

@implementation FFTransferCell

+ (CGSize)contentSizeForMessage:(WKMessageModel *)model {
    return CGSizeMake([WKApp shared].config.messageContentMaxWidth,100.0f);
}

- (void)initUI {
    [super initUI];
    self.messageContentView.layer.masksToBounds = YES;
    self.messageContentView.layer.cornerRadius = 4.0f;
    
    [self.messageContentView addSubview:self.splitLineView];
    [self.messageContentView addSubview:self.iconImgView];
    [self.messageContentView addSubview:self.amountLbl];
    [self.messageContentView addSubview:self.remarkLbl];
    [self.messageContentView addSubview:self.copyrightLbl];
}

- (void)refresh:(WKMessageModel *)model {
    [super refresh:model];
    
    self.trailingView.timeLbl.textColor = [UIColor whiteColor];
    
    FFTransferContent *content = (FFTransferContent*)model.content;
    
    NSInteger status = [self getStatus];
    if(status != LIMTransferStatusForWait) {
        [self.messageContentView setBackgroundColor:[UIColor colorWithRed:255/255.0 green:204/255.0 blue:138/255.0 alpha:1.0]];
    }else {
        [self.messageContentView setBackgroundColor:[UIColor colorWithRed:255/255.0 green:179/255.0 blue:80/255.0 alpha:1.0]];
    }
    
    
    self.amountLbl.text = [NSString stringWithFormat:@"¥%@",[WKMoneyUtil penny2YuanFormat:@(content.amount)]];
    bool hasRemark = content.remark && ![content.remark isEqualToString:@""];
    
    if([content.status isEqualToString:LIMTransferStatusAccepted]) {
        self.remarkLbl.text = LLang(@"已收款");
    }else {
        if(status == LIMTransferStatusForReceived) {
            self.remarkLbl.text = LLang(@"已被领取");
        }else if(status == LIMTransferStatusForExpire) {
            self.remarkLbl.text = LLang(@"已过期");
        }else {
            if(model.isSend) {
                self.remarkLbl.text = [NSString stringWithFormat:LLang(@"转账给%@"),model.channelInfo.displayName];
            }else {
                self.remarkLbl.text = LLang(@"转账给你");
            }
        }
    }
    
    
    if(hasRemark && status == LIMTransferStatusForReceived && ![content.status isEqualToString:LIMTransferStatusAccepted]) {
        self.remarkLbl.text = [NSString stringWithFormat:@"%@-%@",self.remarkLbl.text,content.remark];
    }
    
}

- (void)onTap {
    FFTransferContent *content = (FFTransferContent*)self.messageModel.content;
    FFTransferDetailVC *vc = [FFTransferDetailVC new];
    vc.recordNo = content.recordNo;
    NSInteger status = [self getStatus];
    __weak typeof(self) weakSelf = self;
    if(status==LIMTransferStatusForWait) {
        [vc setOnStatus:^(NSInteger status) {
            if(status != LIMTransferStatusForWait) {
                weakSelf.messageModel.message.extra[@"status"] = @(status);
                [[WKSDK shared].chatManager updateMessageLocalExtra:weakSelf.messageModel.message];
            }
            
        }];
    }
    [[WKNavigationManager shared] pushViewController:vc animated:YES];
}

-(NSInteger) getStatus {
    FFTransferContent *content = (FFTransferContent*)self.messageModel.content;
    if([content.status isEqualToString:LIMTransferStatusAccepted]) {
        return LIMTransferStatusForReceived;
    }
   NSInteger status = self.messageModel.message.extra[@"status"]?[self.messageModel.message.extra[@"status"] integerValue]:LIMTransferStatusForWait;
    return status;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.splitLineView.lim_left = 10.0f;
    self.splitLineView.lim_top = self.messageContentView.lim_height - 20.0f - self.splitLineView.lim_height;
    self.splitLineView.lim_width = self.messageContentView.lim_width - self.splitLineView.lim_left*2.0f;
    
    CGFloat contentHeight = self.splitLineView.lim_top;
    
    self.iconImgView.lim_left = self.splitLineView.lim_left;
    self.iconImgView.lim_top = contentHeight/2.0f - self.iconImgView.lim_height/2.0f;
    
    [self.amountLbl sizeToFit];
    self.amountLbl.lim_left = self.iconImgView.lim_right + 10.0f;
    self.amountLbl.lim_top = self.iconImgView.lim_top - 4.0f;
    
    [self.remarkLbl sizeToFit];
    self.remarkLbl.lim_left = self.amountLbl.lim_left;
    self.remarkLbl.lim_top = self.amountLbl.lim_bottom + 4.0f;
    
    [self.copyrightLbl sizeToFit];
    self.copyrightLbl.lim_left = self.splitLineView.lim_left;
    self.copyrightLbl.lim_top = contentHeight + (self.messageContentView.lim_height - contentHeight)/2.0f - self.copyrightLbl.lim_height/2.0f;
    
    self.trailingView.lim_top = self.messageContentView.lim_height - self.trailingView.lim_height - 4.0f;
}

+ (BOOL)hiddenBubble {
    return YES;
}

- (UIView *)splitLineView {
    if(!_splitLineView) {
        _splitLineView = [[UIView alloc] init];
        _splitLineView.lim_height = 0.5f;
        _splitLineView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:197/255.0 blue:121/255.0 alpha:1.0].CGColor;
    }
    return _splitLineView;
}

- (UIImageView *)iconImgView {
    if(!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 45.0f, 45.0f)];
        [_iconImgView setImage:[self imageName:@"transfer"]];
    }
    return _iconImgView;
}

- (UILabel *)amountLbl {
    if(!_amountLbl) {
        _amountLbl = [[UILabel alloc] init];
        [_amountLbl setTextColor:[UIColor whiteColor]];
        _amountLbl.font = [[WKApp shared].config appFontOfSize:17.0f];
    }
    return _amountLbl;
}

- (UILabel *)remarkLbl {
    if(!_remarkLbl) {
        _remarkLbl = [[UILabel alloc] init];
        [_remarkLbl setTextColor:[UIColor whiteColor]];
        _remarkLbl.font = [[WKApp shared].config appFontOfSize:12.0f];
    }
    return _remarkLbl;
}

- (UILabel *)copyrightLbl {
    if(!_copyrightLbl) {
        _copyrightLbl = [[UILabel alloc] init];
        _copyrightLbl.textColor = [UIColor whiteColor];
        _copyrightLbl.font = [[WKApp shared].config appFontOfSize:10.0f];
        _copyrightLbl.text = [NSString stringWithFormat:LLang(@"%@转账"),[WKApp shared].config.appName];
    }
    return _copyrightLbl;
}



- (UIImage*)imageName:(NSString *)name {
    return [[WKApp shared] loadImage:name moduleID:[FFTransferModule gmoduleId]];
}

@end
